/**
 * 玩家组件
 * by wyy
 */
class player extends eui.Component implements eui.UIComponent {
    public shootLine: eui.Rect;
    public player: eui.Rect;

    private touchBeginX: number = 0;
    private touchBeginY: number = 0;
    public bMove: boolean;
    public shootAngle: number = 0;
    //射击力度
    public shootForce: number = 15;
    public bShooting: boolean = false;

    //玩家座位
    pos: number = 1;


    public shootNum: number = 5;
    constructor(pos = 1) {
        super();
        this.skinName = "resource/eui_skins/game_skins/playerSkin.exml";
        this.pos = pos;
    }
    childrenCreated() {
        //玩家控制
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.move, this);
    }
    public moveBegin(evt) {
        this.touchBeginX = evt.stageX;
        this.touchBeginY = evt.stageY;
        (!this.bShooting) && (this.bMove = true);
    }
    public move(evt) {
        if (this.bMove) {
            if (this.pos <= 4) {
                let moveX = evt.stageX - this.touchBeginX;
                let x = this.x + moveX;
                if (x < (this.player.width / 2)) {
                    x = this.player.width / 2;
                }
                else if (x > (this.parent.width - this.player.width / 2)) {
                    x = this.parent.width - this.player.width / 2;
                }
                this.x = x;
                this.touchBeginX = evt.stageX;
            }
            else if (this.pos > 4) {
                let moveY = evt.stageY - this.touchBeginY;
                let y = this.y + moveY;
                if (y < (this.player.height / 2)) {
                    y = this.player.height / 2;
                }
                else if (y > (this.parent.height - this.player.height / 2)) {
                    y = this.parent.height - this.player.height / 2;
                }
                this.y = y;
                this.touchBeginY = evt.stageY;
            }

        }
    }
    public moveEnd(evt) {
        this.bMove = false;
    }
    //射击标线
    public showShootLine(angle) {
        if (this.bShooting) {
            return;
        }
        this.shootLine.visible = true;
        this.shootLine.rotation = angle / (Math.PI / 180) + 90;
        this.shootAngle = angle;
    }
    /**
     * 释放摇杆
     * @param bShoot true为发射 false为取消发射
     */
    public hideShootLine(bShoot) {
        this.shootLine.visible = false;
        //发射小球
        if ((bShoot) && (!this.bMove)) {
            gameScene.getInstance().shoot();
        }
    }
}